package com.atguigu.eduservice.controller;


import com.aiguigu.commonutils.R;
import com.aiguigu.commonutils.ResultCode;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.vo.VideoInfoVo;
import com.atguigu.eduservice.feign.VodFeignService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuLiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author Y丶HS练习谷粒商城
 * @since 2022-09-24
 */
@Api(description="课时管理")
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {
    /**
     * 小结服务
     */
    @Resource
    private EduVideoService eduVideoService;

    @Resource
    private VodFeignService vodFeignService;

    @ApiOperation("保存小节信息")
    @PostMapping("/saveEntity")
    public R saveEntity(
            @ApiParam(name = "eduVideo", value = "小节对象", required = true)
            @RequestBody VideoInfoVo videoInfoVo) {
        eduVideoService.saveEntity(videoInfoVo);
        return R.ok();
    }

    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("根据id删除实体信息")
    @DeleteMapping("/deleteEntityById/{id}")
    public R deleteEntityById(@PathVariable("id") String id) {
        EduVideo eduVideo = eduVideoService.getById(id);
        if(Objects.isNull(eduVideo)){
            throw new GuLiException(ResultCode.ERROR,"该视屏不存在");
        }else{
            R r = vodFeignService.delAlyVideoById(eduVideo.getVideoSourceId());
            if(r.getCode() == ResultCode.ERROR){
                throw new GuLiException(ResultCode.ERROR,"删除视屏失败，熔断器工作");
            }
            return  eduVideoService.removeById(id) ? R.ok() :R.error();
        }
    }

    @ApiOperation("根据id获取实体信息")
    @GetMapping("/getVideoInfo/{id}")
    public R getVideoInfo(
            @ApiParam("章节id")
            @PathVariable("id") String id
    ){
        return R.ok().data("info", eduVideoService.getById(id));
    }

    @ApiOperation("根据Id修改实体信息")
    @PutMapping("/updateEntity")
    public R updateEntity(
            @ApiParam(name = "eduChapter", value = "小节对象", required = true)
            @RequestBody EduVideo eduVideo) {
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }
}

